package cn.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.dao.CompanyDao;
import cn.dao.BanjiDao;
import cn.dao.PostDao;
import cn.dao.RepeDao;
import cn.entity.TCompany;
import cn.entity.TPost;
import cn.entity.TRepe;
import cn.entity.TStudent;
import cn.entity.TTeacher;
import cn.util.Const;
import cn.util.PageBean;
@Controller("repeAction")
@Scope("prototype")
public class RepeAction extends BaseAction{
	private Integer id;
	private String name;
	private TRepe repe=new TRepe();
	private List<TRepe> list;
	@Autowired
	@Qualifier("repeDao")
	private RepeDao repeDao;

	
	private PageBean pageBean=new PageBean();
	
	public String list(){
		String sqlCount="select count(*) from TRepe u where 1=1";
		String sql="from TRepe u where 1=1";
		if(name!=null && !"".equals(name)){
			sqlCount+=" and u.student.banji.name like '%"+name+"%'";
			sql+=" and u.student.banji.name like '%"+name+"%'";
		}
		//毕业生查看自己的
		if((Integer)session.getAttribute("role")==1){
			TStudent student=(TStudent)session.getAttribute("currentUser");
			sqlCount+=" and u.student.id="+student.getId();
			sql+=" and u.student.id="+student.getId();
		}
		//辅导员查看自己学生的
		if((Integer)session.getAttribute("role")==3){
			TTeacher teacher=(TTeacher)session.getAttribute("currentUser");
			sqlCount+=" and u.student.teacher.id="+teacher.getId();
			sql+=" and u.student.teacher.id="+teacher.getId();
		}
		sqlCount+=" and u.isdel=0";
		sql+=" and u.isdel=0";
		pageBean.setTotalCount(repeDao.getCount(sqlCount));
		list=repeDao.getByPage(sql, pageBean.getPageNo(), pageBean.getPageSize());
		return SUCCESS;
	}
	
	public String listJson() throws IOException{
		String sql="from TRepe u where u.isdel=0";
		list=repeDao.getByHql(sql);
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		TRepe com=repeDao.get(TRepe.class, id);		
		out.print(JSONArray.toJSONString(list));
		out.close();
		return null;
	}
	
	public String add(){
		repe.setIsdel(0);
		repeDao.add(repe);
		return "list";
	}
	
	public String detail() throws IOException{
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		TRepe com=repeDao.get(TRepe.class, id);		
		out.print(JSON.toJSONString(com));
		out.close();
		return null;
	}
	
	public String toupdate(){
		repe=repeDao.get(TRepe.class, id);
		return "toupdate";
	}
	
	public String update(){
		TRepe j=repeDao.get(TRepe.class, id);
		if(repe.getContent()!=null && !"".equals(repe.getContent())){
			j.setContent(repe.getContent());
		}
		if(repe.getRtime()!=null && !"".equals(repe.getRtime())){
			j.setRtime(repe.getRtime());
		}
		repeDao.update(j);
		return "list";
	}
	
	
	
	public String delete(){
		TRepe s=repeDao.get(TRepe.class, id);
		s.setIsdel(1);
		repeDao.update(s);
		return "list";
	}
	


	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}


	public TRepe getRepe() {
		return repe;
	}

	public void setRepe(TRepe repe) {
		this.repe = repe;
	}

	public List<TRepe> getList() {
		return list;
	}

	public void setList(List<TRepe> list) {
		this.list = list;
	}

	public RepeDao getRepeDao() {
		return repeDao;
	}

	public void setRepeDao(RepeDao repeDao) {
		this.repeDao = repeDao;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	

}
